//
//  VisitorView.swift
//  sina----
//
//  Created by mac on 16/5/9.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
class VisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 公共方法, 供其他类调用
    func setupInfo(iconName: String?, lableText: String) {
        //如果iconName为nil, 那么就表示为首界面
        guard let iconName = iconName else {
            startAnimation()
            return
        }
        //iconView不为nil
        textLable.text = lableText
        iconView.image = UIImage(named: iconName)
        
        maskIconView.hidden = true
        houseView.hidden = true
    }
    private func startAnimation() {
        //对那个属性进行动画设置
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //执行动画不会被销毁(在切换界面的时候, 依然保持动画可以执行)
        anim.removedOnCompletion = false
        //给iconView设置动画
        iconView.layer .addAnimation(anim, forKey: nil)
    }
    
    //MARK: - 懒加载
    private lazy var iconView: UIImageView = UIImageView(mc_imageName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskIconView: UIImageView = UIImageView(mc_imageName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var houseView: UIImageView = UIImageView(mc_imageName: "visitordiscover_feed_image_house")
    ///lable文字
    private lazy var textLable: UILabel = UILabel(
        title: "关注一些人，回这里看看有什么惊喜",
        alignment: .Center)
    
    ///登录按钮
    private lazy var longin: UIButton = UIButton(title: "登录", backgroundImageName: "common_button_white_disable")

    ///注册按钮
    private lazy var regist: UIButton = UIButton(
        title: "注册",
        color: UIColor.orangeColor(),
        backgroundImageName: "common_button_white_disable")
    

}
// MARK: - 设置UI
extension VisitorView {

    private func setupUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(textLable)
        addSubview(regist)
        addSubview(longin)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        //系统原生自动布局方法
        self.addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        self.addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: -40))
        //Snapkit 方法
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(self).offset(-50)
        }
        
        houseView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        textLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSizeMake(224, 36))
        }
        
        regist.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(textLable)
            make.top.equalTo(textLable.snp_bottom).offset(10)
            make.size.equalTo(CGSizeMake(100, 36))
        }
        
        longin.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(textLable)
            make.top.equalTo(regist)
            make.size.equalTo(regist)
        }
    }
}
